﻿using DapperExt.Web.Models;
using Dapper;
using DapperExtensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace DapperExt.Web.Controllers
{
    public class TestController : BaseController
    {
      
        // GET: Test
        public ActionResult Index()
        {
            IList<ISort> sort = new List<ISort>();
            sort.Add(new Sort { PropertyName = "Id", Ascending = false });
          //  sort.Add(new Sort { PropertyName = "x_user.UserName", Ascending = false });



            // //条件查询
            //// list = this.GetList<DemoEntity>(new { ID = 1, Name = "123" }, sort);

            // //orm 拼接条件 查询
            IList<IPredicate> predList = new List<IPredicate>();
            //predList.Add(Predicates.Field<x_logEntity>(l => l.LogType, Operator.Eq, "Login"));
            //predList.Add(Predicates.Field<x_logEntity>(l => l.Status, Operator.Eq, 1));
            //predList.Add(Predicates.Field<x_logEntity>(l => l.Id, Operator.Gt, 34));

            predList.Add(Predicates.Field<x_userEntity>(u => u.Status, Operator.Eq, 1));

            IPredicateGroup predGroup = Predicates.Group(GroupOperator.And, predList.ToArray());

            //var predicate = Predicates.Field<x_logEntity>(f => f.CreateOn, Operator.Lt, DateTime.UtcNow.AddDays(-5));

            //var log = new x_logLogic().Read(34);
            //页码需要传-1的数(如第一页需要传0)
            //long count = 0;
            //var logs = Db.GetPage<x_logEntity>(predGroup, sort, 1, 24);
            //var xdd = JsonConvert.SerializeObject(xd);

            //强类型用dtoModel吧
            //var xd = Db.Connection.Query("select x_user.UserName,x_role.RoleName from x_user  INNER JOIN x_role  on x_user.RoleId=x_role.Id ").ToList();
          //  var xd = Db.Connection.Query("select x_user.*,x_role.RoleName from x_user  INNER JOIN x_role  on x_user.RoleId=x_role.Id ");


            string columns = " x_user.*,x_role.RoleName ";
            string joinsql = " x_user  INNER JOIN x_role  on x_user.RoleId=x_role.Id ";//x_user u INNER JOIN x_role r on u.RoleId=r.Id 
  
            var x_u = Db.GetJoinPage<x_userEntity>(columns, joinsql, predGroup, sort, 0, 2);

           var log =  Db.GetPage<x_logEntity>(null, sort, 1, 32);


            // Json(xd[0].Id, JsonRequestBehavior.AllowGet);

            return Content(JsonConvert.SerializeObject(x_u));

            //return View();
        }




        public ActionResult AddGuidTest()
        {
            Models.x_Table model = new Models.x_Table() { Id = Guid.NewGuid(), Name = Guid.NewGuid() };


            var result = Db.Insert<x_Table>(model);


            return Json(result, JsonRequestBehavior.AllowGet);
        }





    }
}